#pragma once

#include "AddVariableValueAction.h"

#include "InputPin.h"

namespace uml {

const bool AddVariableValueAction::isReplaceAll_default_ = false;
AddVariableValueAction::AddVariableValueAction(const QString& umlType)
    : WriteVariableAction(umlType) {}

void AddVariableValueAction::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    WriteVariableAction::fromJson(json, errorList);
    insertAt_ = JsonSerializable::create<InputPin>(json.value("insertAt"), errorList);

    isReplaceAll_ = json.value("isReplaceAll").toBool(isReplaceAll_default_);
}

void AddVariableValueAction::toJson(QJsonObject& json) {
    WriteVariableAction::toJson(json);
    if (insertAt_) {
        QJsonObject insertAt_json;
        insertAt_->toJson(insertAt_json);
        json.insert("insertAt", insertAt_json);
    }
    if (isReplaceAll_ != isReplaceAll_default_) {
        json.insert("isReplaceAll", isReplaceAll_);
    }
}

/// <summary>
/// insertAt getter
/// The InputPin that gives the position at which to insert a new value or move an
/// existing value in ordered Variables. The type of the insertAt InputPin is
/// UnlimitedNatural, but the value cannot be zero. It is omitted for unordered
/// Variables.
/// </summary>
shared_ptr<InputPin> AddVariableValueAction::insertAt() const {
    return insertAt_;
}

/// <summary>
/// insertAt setter
/// The InputPin that gives the position at which to insert a new value or move an
/// existing value in ordered Variables. The type of the insertAt InputPin is
/// UnlimitedNatural, but the value cannot be zero. It is omitted for unordered
/// Variables.
/// </summary>
void AddVariableValueAction::setInsertAt(shared_ptr<InputPin> value) {
    insertAt_ = value;
}

/// <summary>
/// isReplaceAll getter
/// Specifies whether existing values of the Variable should be removed before
/// adding the new value.
/// </summary>
bool AddVariableValueAction::isReplaceAll() const {
    return isReplaceAll_;
}

/// <summary>
/// isReplaceAll setter
/// Specifies whether existing values of the Variable should be removed before
/// adding the new value.
/// </summary>
void AddVariableValueAction::setIsReplaceAll(bool value) {
    isReplaceAll_ = value;
}

} // namespace uml
